package com.example.yichuangyun.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.DialogExamineBinding;
import com.example.yichuangyun.utils.ToastHelper;

/**
 * 废弃审核弹窗
 */
public class ExamineDialog {
    private final Dialog dialog;
    /**
     * 0同意废弃、1拒绝废弃
     */
    private Integer result = null;

    public ExamineDialog(Context context) {
        DialogExamineBinding binding = DialogExamineBinding.inflate(LayoutInflater.from(context));
        dialog = new Dialog(context, R.style.prompt_progress_dialog_dimenabled);
        dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.setCancelable(true);
        Window dialogWindow = dialog.getWindow();
        // 去除遮罩效果
//        dialogWindow.setDimAmount(0f);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.main_menu_animStyle);
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWindow.setAttributes(lp);
        binding.tvLeft.setOnClickListener(v -> {
            leftClick();
            dismissDialog();
        });

        binding.radioGroup.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i == R.id.rb_agree) {
                result = 0;
                binding.etContent.setHint(context.getString(R.string.input_max_count));
            } else {
                result = 1;
                binding.etContent.setHint(context.getString(R.string.input_max_count_not_null));
            }
        });
        binding.tvRight.setOnClickListener(v -> {
            if (result != null) {
                if (result == 1 && TextUtils.isEmpty(binding.etContent.getText().toString().trim())) {
                    ToastHelper.show(context.getString(R.string.input_refuse_reason));
                    return;
                }
                rightClick(binding.etContent.getText().toString().trim(), result);
                dismissDialog();
            } else {
                ToastHelper.show(context.getString(R.string.choose_discard_result));
            }
        });
    }

    public void leftClick() {

    }

    public void rightClick(String content, int result) {

    }

    public void showDialog() {
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
